<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'
import { modelData } from '@/api/modelData'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    const xDate = []
    const series = [
      {
        type: 'line',
        symbolSize: 2,
        symbol: 'emptyCircle',
        showSymbol: false,
        lineStyle: {
          width: 1.6
        },
        data: []
      }
    ]
    modelData.forEach(item => {
      xDate.push(item.date)
      series[0].data.push(item.index_value)
    })
    this.initChart(series, xDate)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(series, xDate) {
      this.chart = echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        grid: {
          top: '10%',
          left: '2%',
          right: '2%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff'
            }
          },
        },
        // xAxis: {},
        // yAxis: {},
        xAxis: {
          gridIndex: 0,
          // min: 'dataMin',
          // max: 'dataMax',
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          splitLine: {
            
            lineStyle: {
              type: 'dashed'
            }
          },
          data: xDate
        },
        yAxis: { 
          gridIndex: 0,
          // min: 'dataMin',
          // max: 'dataMax',
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          },
        },
        series
      })
    }
  }
}
</script>
